<?php

class Core_Model_Permission_Accredit_Action extends Dcmp_Model_Entity {

    public function __construct() {
        parent::__construct();
    }

    protected function getTableClass() {
        return Kd_Loader::loadClass('table_permission_accredit_action');
    }

    protected function insertSelf($transaction) {
        $data = $this->getAllData();
        if ($this->isArrayData('role_ids')) {
            $data['role_ids'] = implode(',', $this->getRoleIds());
        }
        $this->getTable()->insert($data, $transaction);
        $this->setId($this->getTable()->getInsertId());
    }

    protected function afterInsert($transaction) {
        if ($this->getAccessControl() == 'allow_role') {
            $this->getTable()->insertRoleRelation($this->getId(), $this->getRoleIds(), $transaction);
        }
    }

    protected function updateSelf($transaction){
        $data = $this->getChangedData();
        if ($this->propertyIsChanged('role_ids') && $this->isArrayData('role_ids')) {
            $data['role_ids'] = implode(',', $this->getRoleIds());
        }
        $data['id'] = $this->getId();
        $this->getTable()->update($data, $transaction);
    }

    protected function afterUpdate($transaction) {
        $this->getTable()->deleteRoleRelation($this->getId(), $transaction);
        if ($this->getAccessControl() == 'allow_role') {
            $this->getTable()->insertRoleRelation($this->getId(), $this->getRoleIds(), $transaction);
        }
    }

    protected function beforeDelete($transaction){
        $this->getTable()->deleteReleRelation($this->getId(), $transaction);
    }

    public function deleteBatch(array $ids, $transaction = NULL){
        $this->getTable()->deleteBatch($ids);
        return $this;
    }

    public function allowAccess($user){
        if($user->isSuperAdmin()){
            return TRUE;
        }
        switch ($this->getAccessControl()) {
            case 'anonymous':
                return TRUE;
            case 'login':
                return $user->isLogin();
            case 'allow_role':
                foreach ($user->getRoleIds() as $roleId) {
                    if (in_array($roleId, $this->getRoleIds())) {
                        return TRUE;
                    }
                }               
                return FALSE;
            case 'admin':
                return $user->isSuperAdmin();
        }
        return FALSE;
    }

    public function fill($data) {
        if(isset($data['role_ids'])){
             if(!is_array($data['role_ids'])){
                 $data['role_ids'] = explode(',', $data['role_ids']);
             }
        }else{
            $data['role_ids'] = array();
        }
        return parent::fill($data);
    }
    
    protected function initializeTable($table) {
        $table->setName('{t}permission_accredit_action');
        $table->createColumn('id', 'integer', TRUE, TRUE);
        $table->createColumn('access_control', 'string')->setDefaultValue('anonymous');
        $table->createColumn('area_id', 'integer');
        $table->createColumn('component_name', 'string');
        $table->createColumn('controller_name', 'string');
        $table->createColumn('action_name', 'string');
        $table->createColumn('role_ids', 'string');
    }
}